
#pragma once

#include <iostream>
#include <thread>
#include <mutex>
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#include <ctime>
#include <fstream>
#include "json_msg_chassis.h"

// bool prettyWritefile(const char *relatedPath, rapidjson::Document &document)
// {
//     rapidjson::StringBuffer buffer;
//     rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
//     // PrettyWriter  这个词很关键 如果只是Write的话  写入的结构会比较难看，但是更省空间？
//     document.Accept(writer);       // 将json的数据流写入 writer?
//     std::string Path(file_topdir); // 将文件顶层路径转换位std::string 类型
//     Path.append(relatedPath);      // 添加相对路径
//     std::string temp(buffer.GetString());
//     std::ofstream outfile;
//     outfile.open(Path.data());
//     if (outfile.fail())
//     {
//         printf("open file %s error\r\n", Path.data());
//         printf("err here: %s,%d\r\n", __FILE__, __LINE__);
//         return false;
//     }
//     else
//     {
//         outfile << temp;
//         outfile.close();
//         return true;
//         // 关于文件写入部分，确实不熟悉，不去过多的解释
//     }
// }

namespace tbox_msg
{
    bool jsonMsgRepChassis::jsonDocTostring(std::string &jToStr)
    {
        rapidjson::StringBuffer buffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
        doc.Accept(writer);
        jToStr = buffer.GetString();
        return true;
    }


    void jsonMsgRepChassis::changeReportJson()
    {
        time_t timestamp = time(nullptr);
        doc["VIN"].SetString(rapidjson::StringRef(VIN.c_str()));
        doc["terminalID"].SetString("02");
        doc["frameID"].SetInt(123);
        doc["sysDataType"].SetInt(1002);
        doc["timestamp"].SetInt64(timestamp);
        doc["code"].SetString("01");

        doc["data"]["RCU"].SetString(rapidjson::StringRef(RCU.c_str()));
        doc["data"]["BCM"].SetString(rapidjson::StringRef(BCM.c_str()));
        doc["data"]["DC"].SetString(rapidjson::StringRef(DC.c_str()));
        doc["data"]["EHB1"].SetString(rapidjson::StringRef(EHB1.c_str()));
        doc["data"]["EHB2"].SetString(rapidjson::StringRef(EHB2.c_str()));
        doc["data"]["EPB1"].SetString(rapidjson::StringRef(EPB1.c_str()));
        doc["data"]["EPB2"].SetString(rapidjson::StringRef(EPB2.c_str()));
        doc["data"]["EPS1"].SetString(rapidjson::StringRef(EPS1.c_str()));
        doc["data"]["EPS2"].SetString(rapidjson::StringRef(EPS2.c_str()));
        doc["data"]["BMS1"].SetString(rapidjson::StringRef(BMS1.c_str()));
        doc["data"]["BMS2"].SetString(rapidjson::StringRef(BMS2.c_str()));
        doc["data"]["BMS3"].SetString(rapidjson::StringRef(BMS3.c_str()));
        doc["data"]["BMS4"].SetString(rapidjson::StringRef(BMS4.c_str()));
        doc["data"]["MCU1"].SetString(rapidjson::StringRef(MCU1.c_str()));
        doc["data"]["MCU2"].SetString(rapidjson::StringRef(MCU2.c_str()));
        doc["data"]["VEH1"].SetString(rapidjson::StringRef(VEH1.c_str()));
        doc["data"]["VEH2"].SetString(rapidjson::StringRef(VEH2.c_str()));
        doc["data"]["VEH3"].SetString(rapidjson::StringRef(VEH3.c_str()));
        doc["data"]["event"].SetString(rapidjson::StringRef(event.c_str()));
        doc["data"]["TBOX_OK"].SetString(rapidjson::StringRef(TBOX_OK.c_str()));
        doc["data"]["TBOX_sleep"].SetString(rapidjson::StringRef(TBOX_sleep.c_str()));
    }

    void jsonMsgRepChassis::initReportJson()
    {
        doc.SetObject();
        doc.AddMember(rapidjson::StringRef("VIN"), rapidjson::StringRef(VIN.c_str()), doc.GetAllocator());
        doc.AddMember(rapidjson::StringRef("terminalID"), 0, doc.GetAllocator());
        doc.AddMember(rapidjson::StringRef("frameID"), 10, doc.GetAllocator());
        doc.AddMember(rapidjson::StringRef("sysDataType"), 1002, doc.GetAllocator());
        doc.AddMember(rapidjson::StringRef("timestamp"), 1706534200, doc.GetAllocator());
        doc.AddMember(rapidjson::StringRef("code"), "01", doc.GetAllocator());
        rapidjson::Value val_dataObject(rapidjson::kObjectType);
        val_dataObject.AddMember(rapidjson::StringRef("prop"), rapidjson::StringRef(VIN.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("RCU"), rapidjson::StringRef(RCU.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("BCM"), rapidjson::StringRef(BCM.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("DC"), rapidjson::StringRef(DC.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EHB1"), rapidjson::StringRef(EHB1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EHB2"), rapidjson::StringRef(EHB2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EPB1"), rapidjson::StringRef(EPB1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EPB2"), rapidjson::StringRef(EPB2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EPS1"), rapidjson::StringRef(EPS1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("EPS2"), rapidjson::StringRef(EPS2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("BMS1"), rapidjson::StringRef(BMS1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("BMS2"), rapidjson::StringRef(BMS2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("BMS3"), rapidjson::StringRef(BMS3.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("BMS4"), rapidjson::StringRef(BMS4.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("MCU1"), rapidjson::StringRef(MCU1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("MCU2"), rapidjson::StringRef(MCU2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("VEH1"), rapidjson::StringRef(VEH1.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("VEH2"), rapidjson::StringRef(VEH2.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("VEH3"), rapidjson::StringRef(VEH3.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("event"), rapidjson::StringRef(event.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("TBOX_OK"), rapidjson::StringRef(TBOX_OK.c_str()), doc.GetAllocator());
        val_dataObject.AddMember(rapidjson::StringRef("TBOX_sleep"), rapidjson::StringRef(TBOX_sleep.c_str()), doc.GetAllocator());
        doc.AddMember("data", val_dataObject, doc.GetAllocator());
    }

    void jsonMsgRepChassis::displayJson()
    {
        rapidjson::StringBuffer jsonBuffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(jsonBuffer);
        doc.Accept(writer);
        printf("%s\n\n", jsonBuffer.GetString());
    }

}

// int main_1()
// {

//     std::cout << "\n添加 json" << std::endl;
//     rapidjson::Document doc;

//     initReportJson(doc);
//     displayJson(doc);

//     std::cout << "\n修改 json" << std::endl;
//     changeReportJson(doc);
//     displayJson(doc);

//     std::cout << "\n\n\nfinished." << std::endl;
//     return 0;
// }
